#include "stdafx.h"
#include <serialization/ioParamStructs.h>
#include <serialization/serialization.h>
#include <memory>
#include <winsock/winsock_stub.h>

void getsockopt_stub (
    __in  std::vector<char>& in_buf,
    __out std::vector<char>& out_buf
    )
{
//RECEIVE()
    Deserialize deserialize (in_buf);
    getsockopt_in_inout getsockoptIN;
    deserialize (getsockoptIN);
    int ret;

    if (getsockoptIN.optlen_)
    {
        std::auto_ptr<char> optval(new char[*getsockoptIN.optlen_]);

        ret = getsockopt (getsockoptIN.s_, getsockoptIN.level_, 
                    getsockoptIN.optname_, optval.get(), getsockoptIN.optlen_ );
    //SEND
        getsockopt_out_inout getsockoptOUT( optval.get(), getsockoptIN.optlen_,
            (ret == 0) ? 0 : WSAGetLastError() );
        
        Serialize(deserialize.get_header(), getsockoptOUT, out_buf);
    }
    else
    {
        ret = getsockopt (getsockoptIN.s_, getsockoptIN.level_, 
                          getsockoptIN.optname_, NULL, getsockoptIN.optlen_ );
    //SEND
        getsockopt_out_inout getsockoptOUT( NULL, getsockoptIN.optlen_,
            (ret == 0) ? 0 : WSAGetLastError() );
        
        Serialize(deserialize.get_header(), getsockoptOUT, out_buf);
    }
}